
!> \brief Contains some subroutines to print the collected data in several
!! formats
module mprint

   use data_collector

   implicit none

   !character (len=300), private

contains
   !> \brief Print data into a .csv file
   subroutine print_csv( nf, nv, nlen, filelist, varlist)
      implicit none
      integer :: nf   !< number of files in the file list
      integer :: nv   !< number of variables to be read
      integer :: nlen !< String length
      character (len=nlen) :: filelist(nf) !< list of files
      character (len=nlen) :: varlist(nv)  !< list of variales

      ! Inner variables

      integer :: i, j, res
      character (len=nlen) str


      open(17,file="./output_files/output_file.csv")
      !
      ! Extracting heading
      !
      do j = 1, nv

         if ( j == nv ) then

            write(17,"(A)") trim(adjustl(varlist(j)))

         else

            write(17,"(A,'&')",advance='no') trim(adjustl(varlist(j)))

         end if

      end do


      !
      ! Extracting data
      !

      do i = 1, nf

         do j = 1, nv

            call get_a_string_from_a_file(trim(adjustl(filelist(i))), &
               trim(adjustl(varlist(j))), str, res)

            if ( j == nv ) then

               write(17,"(A)") trim(adjustl(str))

            else

               write(17,"(A,'&')",advance='no') trim(adjustl(str))

            end if

         end do

      end do

      close(17)

   end subroutine


   !> \brief Print data into a .tex file
   subroutine print_tex( nf, nv, nlen, filelist, varlist)
      implicit none
      integer :: nf   !< number of files in the file list
      integer :: nv   !< number of variables to be read
      integer :: nlen !< String length
      character (len=nlen) :: filelist(nf) !< list of files
      character (len=nlen) :: varlist(nv)  !< list of variales

      ! Inner variables

      integer :: i, j, res
      character (len=nlen) str


      open(17,file="./output_files/output_file.tex")
      !
      ! Extracting heading
      !
      do j = 1, nv

         if ( j == nv ) then

            write(17,"(A,' \\')") trim(adjustl(varlist(j)))

         else

            write(17,"(A,'&')",advance='no') trim(adjustl(varlist(j)))

         end if

      end do


      !
      ! Extracting data
      !

      do i = 1, nf

         do j = 1, nv

            call get_a_string_from_a_file(trim(adjustl(filelist(i))), &
               trim(adjustl(varlist(j))), str, res)

            if ( j == nv ) then

               write(17,"(A,' \\')") trim(adjustl(str))

            else

               write(17,"(A,'&')",advance='no') trim(adjustl(str))

            end if

         end do

      end do

      close(17)

   end subroutine


end module
